// CadToolCircle.cpp
//
#include "CadToolCircle.h"

#include "CadEngine.h"

#include "OpenGL/Box.h"
#include "OpenGL/OpenGL.h"
#include "OpenGL/Renderer.h"

//-------------------------------------------------------------------------------------------------
CadToolCircle::CadToolCircle()
: CadTool("Circle",2)
{}

//-------------------------------------------------------------------------------------------------
CadToolCircle::~CadToolCircle()
{}

//-------------------------------------------------------------------------------------------------
void CadToolCircle::execute()
{
	if( OpenGL::Math::distance(_points[0],_points[1]) < _manager->pixelSize() )
		return;

	CadEngine::Command c;

	c.command   = CadEngine::Create;
	c.primitive = CadEngine::Circle;
	c.color     = OpenGL::Color::blue();
	c.points.push_back( _points[0] );
	c.points.push_back( _points[1] );
	c.attributes += _objectAttributes;

	_manager->issueCommand(c);
}

//-------------------------------------------------------------------------------------------------
void CadToolCircle::draw( OpenGL::Renderer *r )
{
	if(_nPoints < 2) return;

	OpenGL::Math::dvec3 o = r->origin();
	OpenGL::Math::dvec3 p0( _points[0].x,_points[0].y,0 );//-r->origin();
	OpenGL::Math::dvec3 p1( _points[1].x,_points[1].y,0 );//-r->origin();

	glPushAttrib( GL_LINE_BIT|GL_ENABLE_BIT );
	glEnable(GL_LINE_STIPPLE);
	glLineStipple(1, 0x00FF);
	glLineWidth(2);	
	glColor4fv( _manager->settings().previewColor().rgba() );

	glBegin( GL_LINES );
		glVertex2dv( OpenGL::Math::value_ptr(p0-o) );
		glVertex2dv( OpenGL::Math::value_ptr(p1-o) );
	glEnd();


	GLUquadric *q = gluNewQuadric();


	glPushMatrix();
		glTranslated( p0.x-o.x,p0.y-o.y,p0.z-o.z );
		
		gluQuadricDrawStyle( q,GLU_SILHOUETTE );
		gluDisk(q,0,OpenGL::Math::distance(p0,p1),64,1);
	glPopMatrix();


	gluDeleteQuadric(q);

	glPopAttrib();
}